package module.map.operations.propertys
{
	import drongo.model.core.IProperty;
	import drongo.model.values.CollectionValue;
	
	import common.operations.BaseOperation;
	
	import module.map.model.Propertys;
	import module.map.operations.propertys.datas.PropertyChangeData;
	
	/**
	 * 集合属性修改
	 */
	public class CollectionPropertyChangedOperation extends BaseOperation
	{
		private var oldState:uint;
		private var parent:CollectionValue;
		private var newPropertys:Vector.<PropertyChangeData>;
		private var oldPropertys:Vector.<PropertyChangeData>;
		public function CollectionPropertyChangedOperation()
		{
			super();
		}
		
		override public function destroy():void
		{
			this.newPropertys.length=0;
			this.newPropertys=null;
			this.oldPropertys.length=0;
			this.oldPropertys=null;
			this.parent=null;
		}
		
		override public function execute():void
		{
			this.parent=this.data.parent;
			this.newPropertys=this.data.newPropertys;
			this.oldPropertys=this.data.oldPropertys;
			if(this.parent==null){
				throw new Error("数据父级不存在！");
			}
			if(this.newPropertys==null||this.newPropertys.length==0){
				throw new Error("新属性不能为空！");
			}
			if(this.oldPropertys==null||this.oldPropertys.length==0){
				throw new Error("老属性不能为空！");
			}
			this.oldState=this.project.currentMap.Get(Propertys.STATE).GetValue();
			this.SetPropertys(this.newPropertys);
			this.project.currentMap.Update(Propertys.STATE,1);
			this.project.currentMap.recorder.link(this);
		}
		
		private function SetPropertys(datas:Vector.<PropertyChangeData>):void{
			var changeData:PropertyChangeData;
			var property:IProperty;
			var changedList:Array=[];
			for (var i:int = 0; i < datas.length; i++) 
			{
				changeData=datas[i];
				this.parent.Update(changeData.key,changeData.value);
				changedList.push(this.parent.Get(changeData.key));
			}
//			this.parent.dispatchEvent(CollectionPropertyEvent.Create(CollectionPropertyEvent.UPDATE,changedList));
		}
		
		override public function redo():void
		{
			this.oldState=this.project.currentMap.Get(Propertys.STATE).GetValue();
			this.project.currentMap.Update(Propertys.STATE,1);
			this.SetPropertys(this.newPropertys);
		}
		
		override public function undo():void
		{
			var state:uint=this.project.currentMap.Get(Propertys.STATE).GetValue();
			if(state==0){
				this.project.currentMap.Update(Propertys.STATE,1);
			}else{
				this.project.currentMap.Update(Propertys.STATE,this.oldState);
			}
			this.SetPropertys(this.oldPropertys);
		}
	}
}